package aplicacion;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UnsupportedLookAndFeelException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Aplicacion.java
 *
 * Created on 27-08-2010, 09:40:54 PM
 */

/**
 *
 * @author Administrador
 */
public class Aplicacion extends javax.swing.JFrame {

    private String nombreSO = System.getProperty("os.name");
    private String comando;
    private Process proceso;
    private String salida = "";
    private String error = "";
    private String historial = "";

    /** Creates new form Aplicacion */
    public Aplicacion() {
        initComponents();
        this.sis_operativo.setText(nombreSO);
        this.sis_operativo.setEditable(false);
        this.area_salida.setEditable(false);
        this.err.setText("");
        initAcercaDe();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        sis_operativo = new javax.swing.JTextField();
        com_entrada = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        area_salida = new java.awt.TextArea();
        jLabel4 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        txt_his = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        err = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menuItemAcercaDe = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Aplicacion Comandos");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jLabel1.setText("Sistema Operativo Actual: ");

        sis_operativo.setBackground(new java.awt.Color(1, 1, 1));
        sis_operativo.setFont(new java.awt.Font("DejaVu Sans", 0, 18));
        sis_operativo.setForeground(new java.awt.Color(255, 0, 200));
        sis_operativo.setHorizontalAlignment(javax.swing.JTextField.LEFT);

        com_entrada.setBackground(new java.awt.Color(1, 1, 1));
        com_entrada.setFont(new java.awt.Font("DejaVu Sans", 0, 14));
        com_entrada.setForeground(new java.awt.Color(255, 244, 0));
        com_entrada.setText(" ");
        com_entrada.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                com_entradaKeyPressed(evt);
            }
        });

        jLabel2.setText("Comando:");

        area_salida.setBackground(new java.awt.Color(0, 0, 0));
        area_salida.setFont(new java.awt.Font("Nimbus Mono L", 0, 14));
        area_salida.setForeground(new java.awt.Color(51, 255, 0));

        jLabel4.setText("Error:");

        txt_his.setBackground(new java.awt.Color(1, 1, 1));
        txt_his.setColumns(20);
        txt_his.setFont(new java.awt.Font("Nimbus Mono L", 0, 14));
        txt_his.setForeground(new java.awt.Color(255, 255, 0));
        txt_his.setRows(5);
        jScrollPane1.setViewportView(txt_his);

        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 1, 13));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Historial de Comandos");

        err.setBackground(new java.awt.Color(1, 1, 1));
        err.setColumns(20);
        err.setForeground(new java.awt.Color(255, 10, 0));
        err.setRows(5);
        jScrollPane2.setViewportView(err);

        jMenu1.setText("Ayuda");

        menuItemAcercaDe.setText("Acerca de...");
        jMenu1.add(menuItemAcercaDe);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 817, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 785, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sis_operativo, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 817, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(area_salida, javax.swing.GroupLayout.DEFAULT_SIZE, 627, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(com_entrada, javax.swing.GroupLayout.DEFAULT_SIZE, 764, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(sis_operativo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(com_entrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(2, 2, 2))
                    .addComponent(area_salida, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE))
                .addGap(35, 35, 35))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void com_entradaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_com_entradaKeyPressed
        if(evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER){
            comando = this.com_entrada.getText();
            if(!historial.contains(comando)){
                historial = historial + comando + "\n";
                this.txt_his.setText(historial);
            }
            com_entrada.setText("");
            salida = "";
            error = "";
            try{
                if(this.nombreSO.equalsIgnoreCase("windows xp")){
                    proceso = Runtime.getRuntime().exec("cmd.exe /C "+comando);
                }else{
                    proceso = Runtime.getRuntime().exec(comando);
                }

                BufferedReader brStdOut = new BufferedReader(new InputStreamReader(proceso.getInputStream()));
                BufferedReader brStdErr = new BufferedReader(new InputStreamReader(proceso.getErrorStream()));
                String str=null;
                
                while ((str = brStdOut.readLine())!=null){
                    salida = salida + str + "\n";
                }
                while ((str = brStdErr.readLine())!=null){
                    error = error + str + "\n";
                }

                this.area_salida.setText(salida);
                this.err.setText(error);
                brStdOut.close();
                brStdErr.close();
            }catch(Exception e){
            }


        }
    }//GEN-LAST:event_com_entradaKeyPressed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Aplicacion().setVisible(true);
            }
        });
    }

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.TextArea area_salida;
    private javax.swing.JTextField com_entrada;
    private javax.swing.JTextArea err;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JMenuItem menuItemAcercaDe;
    private javax.swing.JTextField sis_operativo;
    private javax.swing.JTextArea txt_his;
    // End of variables declaration//GEN-END:variables

    private void initAcercaDe() {
        acercade.AcercaDe ad = new acercade.AcercaDe("Aplicación Comandos", "Programa para ejecutar comandos segun su sistema operativo", "Patricio Pérez Pinto", "2012", "patricio.perez.pinto@gmail.com", acercade.GPL.AMBAS_VERSIONES);
        ad.addComponent(this.menuItemAcercaDe);
    }

}
